/*
 * Copyright (c) 2006-2021, RT-Thread Development Team
 *
 * SPDX-License-Identifier: Apache-2.0
 *
 * Change Logs:
 * Date           Author       Notes
 * 2021-12-03     GC       the first version
 */
#include "public.h"
#include "rfid.h"
#include "ble.h"
//开门状态标志位door_flag == 0,关门， door_flag == 1,开门

//创建事件集
rt_event_t event1;

#define EVENT_FLAG_1    (0x1<<0)
#define EVENT_FLAG_2    (0x1<<1)



//将线程初始化加入到系统初始化中
//INIT_APP_EXPORT(Thread_RGB);
INIT_APP_EXPORT(Thread_OLED);


/*
 * LED线程使用部分*/
//创建线程函数入口
static void rgb_thread_entry(void *p)
{
    RGB_Init();
    while(1)
    {
        //如果开门绿灯亮，关门红灯亮
        if(door_flag == 1)
        {
            RGB_Green(1);
            RGB_Red(0);
            rt_thread_mdelay(500);
        }
        else {
            RGB_Red(1);
            RGB_Green(0);
            rt_thread_mdelay(500);
        }

    }
}
/*
函数功能：led线程
函数形参：void
函数返回值：void
函数说明：线程优先级为  10  时间片为10
*/
int Thread_RGB(void)
{
    rt_thread_t rgb_thread = RT_NULL;
    //线程启动函数
    //线程的名字，线程入口函数，线程入口函数参数，线程栈的大小，线程优先级，线程时间片
    rgb_thread = rt_thread_create("rgb",rgb_thread_entry,RT_NULL,512,9,10);
    if(rgb_thread == RT_NULL)
    {
        rt_kprintf("thread_RGB ERROR");
        return RT_ERROR;
    }
    rt_thread_startup(rgb_thread);
    rt_kprintf("thread_RGB success.\n");
}

/*
 OLED线程控制部分
 动态创建线程
创建oled线程函数入口
*/
static void oled_thread_entry(void *p)
{
    OLED_Init();
    //判断门是否开
    OLED_ShowCHinese(40,0,9);//信
    OLED_ShowCHinese(80,0,10);//控
    OLED_ShowString(5,2,"Door state:",16);
    while(1)
    {
        if(door_flag == 1)
        {
            OLED_ShowString(30,4,"Open ",16);
            uart_sendstring(lock_on);
            rt_thread_mdelay(200);
        }
        else {
            OLED_ShowString(30,4,"Close",16);
            uart_sendstring(lock_off);
            rt_thread_mdelay(200);
        }
       // rt_thread_mdelay(500);
    }
}
/*
 *函数功能：初始化OLED线程
 *函数形参: void
 *函数返回值：int
 *函数说明：线程优先级 9  时间片为10
 */
int Thread_OLED(void)
{
    //oled线程控制块指针
    rt_thread_t OLED_thread = RT_NULL;
   //线程启动函数
   //线程的名字，线程入口函数，线程入口函数参数，线程栈的大小，线程优先级，线程时间片
    OLED_thread = rt_thread_create("oled",oled_thread_entry,RT_NULL,512,8,10);
   if(OLED_thread == RT_NULL)
   {
       rt_kprintf("thread_RGB ERROR");
       return RT_ERROR;
   }
   rt_thread_startup(OLED_thread);
   rt_kprintf("OLED_thread success.\n");
}

/*
 RFID线程控制部分
 动态创建线程
 */
/*创建oled线程函数入口*/
static void RFID_thread_entry(void *p)
{
    uint8_t ID[4] = {0};
    RC522_Init();
    while(1)
    {
        //判断门是否开
        Read_Card_ID(ID);
        if(data.rfid_flag == 1)
        {
            Storage_ID_Date(ID);
            Compare_To_Card(ID);
        }

        rt_thread_mdelay(500);
    }
}

/*
函数功能：rfid线程
函数形参：void
函数返回值：void
函数说明：线程优先级为   7  时间片为10
*/
int Thread_rfid(void)
{
    //oled线程控制块指针
    rt_thread_t RFID_thread = RT_NULL;
   //线程启动函数
   //线程的名字，线程入口函数，线程入口函数参数，线程栈的大小，线程优先级，线程时间片
    RFID_thread = rt_thread_create("rfid",RFID_thread_entry,RT_NULL,512,7,10);
   if(RFID_thread == RT_NULL)
   {
       rt_kprintf("thread_RGB ERROR");
       return RT_ERROR;
   }
   rt_thread_startup(RFID_thread);
   rt_kprintf("RFID_thread success.\n");
}
INIT_APP_EXPORT(Thread_rfid);

/*
 key线程控制部分
 动态创建线程
创建key线程函数入口
*/
static void key_thread_entry(void *p)
{
    Key_INIT();
    while(1)
    {
        if(Read_S4() == RT_NULL)        //按键按下，关闭门锁
        {
            door_flag = 0;  //关门
            beep_flag = 2;  //关门
            rt_kprintf("key_s4 off %d\r\n",Read_S2());
        }
        rt_thread_mdelay(200);
    }
}
/*
函数功能：key线程
函数形参：void
函数返回值：void
函数说明：线程优先级为   8  时间片为10
*/
int Thread_key(void)
{
    //key线程控制块指针
    rt_thread_t key_thread = RT_NULL;
    key_thread = rt_thread_create("key",key_thread_entry,RT_NULL,512,6,10);
   if(key_thread == RT_NULL)
   {
       rt_kprintf("thread_RGB ERROR");
       return RT_ERROR;
   }
   rt_thread_startup(key_thread);
   rt_kprintf("key_thread success.\n");
}

INIT_APP_EXPORT(Thread_key);

//RGB彩灯运行线程
static void motor_thread_entry(void *p)
{
    RGB_Init();
    while(1)
    {
        if(door_flag==1)
        {
            //rt_kprintf("open\r\n");
            RGB_Blue(1);
            rt_thread_mdelay(2);
            RGB_Blue(0);
            rt_thread_mdelay(18);
        }
        else {
           // rt_kprintf("close\r\n");
            RGB_Blue(1);
            rt_thread_mdelay(1);
            RGB_Blue(0);
            rt_thread_mdelay(18);
        }

        rt_thread_mdelay(2000);
    }
}
/*
创建线程启动函数，用于启动上一步编写的线程主体
*/
int Thread_motor(void)
{
    rt_thread_t motor_thread = RT_NULL;

    motor_thread = rt_thread_create("RGB", motor_thread_entry, RT_NULL, 512, 4, 10);
    if(motor_thread == RT_NULL)
    {
        rt_kprintf("motor_thread INIT ERROR\n");
        return RT_ERROR;
    }
    else {
        rt_thread_startup(motor_thread);  //启动线程
    }
}
//INIT_APP_EXPORT(Thread_motor);    //线程自动初始化
